# -*- coding: utf-8 -*-
from abc import ABC

from app.models import db_session
from app.models.cod import Error
from app.views import CommonHandler
from app.models.model import MineModel


class MineHandler(CommonHandler, ABC):
    async def get(self):
        mine_code = self.get_argument("mine_code", None)
        query = db_session().query(MineModel)
        mines = query.filter(MineModel.mine_code == mine_code).all()
        if len(mines) < 1:
            await self.response(error=Error.NotData)
            return
        await self.response(data=mines[0])

    async def post(self):
        params, cont = self.json_params(), {}
        name, no, size = params['name'], params['no'], params['size']
        query = db_session().query(MineModel)
        query = query.filter(MineModel.mine_name.like('%'+name+'%'))
        cont['total'] = query.count()
        mines = query.limit(size).offset((no - 1) * size)
        cont['data'] = [mine.info() for mine in mines]
        await self.response(data=cont)
